__author__ = 'Ich'
import pygame
from enemies import Enemies
from shot import Shot
from constants import Constants

pygame.init()

screen = pygame.display.set_mode((Constants.getInfo()[0], Constants.getInfo()[1]))
done = False
x = 1024/2
y = 786-65
clock = pygame.time.Clock()
surface = pygame.Surface((100, 100), pygame.SRCALPHA)
image = pygame.image.load('ball.png')
shot  = None
shot_objx = 0
shot_objy = 0
gegner = []
enemy = None
enemy2 = None
gegneranzahl = 2
score = 0
direction = 2
start = 1

while not done:

        # will block execution until 1/60 seconds have passed
        # since the previous time clock.tick was called.
        clock.tick(60)
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True

        #Spiel
        # This is a list of 'sprites.' Each block in the program is
        # added to this list.
        # The list is managed by a class called 'Group.'
        enemy_list = pygame.sprite.Group()
        # This is a list of every sprite.
        # All blocks and the player block as well.
        all_sprites_list = pygame.sprite.Group()

        screen.fill((0, 0, 0))
        #pygame.draw.rect(screen, (255,0,0), pygame.Rect(x,y,20,20))
        screen.blit(pygame.image.load('ball.png'), (x, y))

        pressed = pygame.key.get_pressed()
        #if pressed[pygame.K_UP]: y -= 3
        #if pressed[pygame.K_DOWN]: y += 3
        if pressed[pygame.K_LEFT] and (x > 0):
            x -= 10
        if pressed[pygame.K_RIGHT] and (x < Constants.getInfo()[0]-50):
            x += 10

        if pressed[pygame.K_UP] and (shot == None):
            shot_objx = x+20;
            shot_objy = y-20;
            shot = Shot(shot_objx,shot_objy)
            all_sprites_list.add (shot)
            #shot = screen.blit(shot_obj, (shot_objx, shot_objy))

        if start == 1:
            enemy = Enemies(600,200)
            enemy2 = Enemies(600,20)
            all_sprites_list.add (enemy)
            enemy_list.add (enemy)
            all_sprites_list.add (enemy2)
            enemy_list.add (enemy2)
            start = 0
        else:
            if not enemy == None:
                all_sprites_list.add (enemy)
                enemy_list.add (enemy)
                enemy.move(1,7) # Bewegen, Richtung (0 Links, 1 Rechts) und Geschwindigkeit
            if not enemy2 == None:
                all_sprites_list.add (enemy2)
                enemy_list.add (enemy2)
                enemy2.move(1,9) # Bewegen, Richtung (0 Links, 1 Rechts) und Geschwindigkeit





        if shot:
            all_sprites_list.add (shot)
            shot.rect.y -= 20

            #returns list
            hitlist = pygame.sprite.spritecollide(shot,enemy_list,True)
            for shot in hitlist:
                score +=1
                print(score)
                print (shot)
                shot = None


            if shot and (shot.rect.x > Constants.getInfo()[0] or shot.rect.y > Constants.getInfo()[1]):
                shot = None
            if shot and (shot.rect.x < 0 or shot.rect.y < 0):
                shot = None
            #
            # #is_a_collision = pygame.sprite.collide_mask(img, gegnerobj)

        all_sprites_list.draw(screen)

        pygame.display.update() #Screen refresh
